﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ProcDaemon
{
    public class ProcessInfo
    {
        private PerformanceCounter _performanceCounter;
        int _reScanSeconds;
        private bool _isException;
        private DateTime _exceptionTime;

        public ProcessInfo(string path, string args, bool autoStart, double cpuPercentageThreshold, double mailSecondsThreshold, double secondsThreshold)
        {
            Path = path;
            Arguments = args;
            AutoStart = autoStart;
            CpuPercentageThreshold = cpuPercentageThreshold;
            MailSecondsThreshold = mailSecondsThreshold;
            SecondsThreshold = secondsThreshold;
            _performanceCounter = new PerformanceCounter("Process", "% Processor Time", ProcessName);
            IsFirstRunPerformance = true;

            _reScanSeconds = int.Parse(ConfigurationManager.AppSettings["ReScanWaitSecondsAfterException"]);
        }

        public int ReScanSeconds
        {
            get
            {
                return _reScanSeconds;
            }
        }

        public bool IsException
        {
            get
            {
                return _isException;
            }
            set
            {
                _isException = value;
                _exceptionTime = DateTime.Now;
            }
        }

        public bool CanReScan()
        {
            if (_isException)
            {
                bool ret = (DateTime.Now - _exceptionTime).TotalSeconds > _reScanSeconds;
                if (ret)
                {
                    _isException = false;
                }

                return ret;
            }

            return true;
        }

        public string Path { get; set; }

        public string Arguments { get; set; }

        public bool AutoStart { get; set; }

        public string ProcessName
        {
            get
            {
                return Path.Substring(Path.LastIndexOf('\\') + 1, Path.LastIndexOf('.') - Path.LastIndexOf('\\') - 1);
            }
        }

        public double CpuPercentageThreshold { get; set; }

        public double SecondsThreshold { get; set; }

        public double MailSecondsThreshold { get; set; }

        public bool ThisTimeHasMailed { get; set; }

        public PerformanceCounter PerformanceCounter
        {
            get
            {
                return _performanceCounter;
            }
        }

        public bool IsFirstRunPerformance { get; set; }

        public DateTime? OvertThresholdBeginTime { get; set; }

        public int CursorLeft { get; set; }

        public int CursorTop { get; set; }

        public override string ToString()
        {
            return string.Format("进程: {0}, CPU阈值: {1}, 超限发送邮件阈值:{2}, 超限重启时间阈值: {3}",
                ProcessName, CpuPercentageThreshold, MailSecondsThreshold, SecondsThreshold);
        }
    }
}
